﻿namespace SocketGateway.DTO
{
    /// <summary>
    /// layui基本数据
    /// </summary>
    public class LayuiResultDto<TData> : BaseResultDto where TData : class
    {
        /// <summary>
        /// 数据载体
        /// </summary>
        public TData Data { get; set; }

        /// <summary>
        /// 总数
        /// </summary>
        public long Total { get; set; }

        /// <summary>
        /// 成功,假如数据是数据组，总数自动赋值；集合的话，需要手动输入
        /// </summary>
        public static LayuiResultDto<Data> Success<Data>(Data data, long total = 0) where Data : class
        {
            var result = new LayuiResultDto<Data> { Data = data, Total = total };
            if (data != null)
            {
                var dataType = typeof(Data);
                if (dataType.IsArray)
                    result.Total = (data as Array).LongLength;
            }

            return result;
        }
    }
}
